package com.cuz.hot100;

import java.util.Arrays;

public class Q9isPalindromeNum {
    public static void main(String[] args) {

        class A {
            public boolean isPalindrome(int x) {
                if(x<10){
                    return x>=0;
                }

                int bounds = 10;
                while(x/bounds!=0){
                    bounds = bounds*10;
                }
                bounds = bounds/10;


                int temp = x;

                while(temp >=10){

                    int first = temp / bounds;
                    int last = temp%10;

                    if(first!=last){
                        return false;
                    }

                    temp -= last;
                    temp -= first*bounds;
                    bounds=bounds/10;
                    temp = temp/10;
                }

                return true;
            }
        }

        System.out.println(new A().isPalindrome(1000021));
        System.out.println(isPalindrome1(121));
        System.out.println(isPalindrome1(1221));
    }

    public static boolean isPalindrome1(int x) {
        if (x < 0) {
            return false;
        }
        if (x <= 9) {
            return true;
        }
        //121
        //12 21
        int digits = digitsOf(x);
        int left = digits / 2;
        int right = left;
        if ((digits &1) != 0) {
            left += 1;
        }
        //1
        //21
        int tail = x % (int) (Math.pow(10, left));
        int head = 0;
        //right=1
        //right=3
        while (right < digits) {
            int temp = x / (int) (Math.pow(10, right));
            head += (int) Math.pow(10, digits - right - 1) * (temp % 10);
            right++;
        }
        return tail == head;
    }

    private static int digitsOf(int x) {
        int res = 0;
        while (x > 9) {
            x = x / 10;
            res++;
        }
        return res + 1;
    }

}
